clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
/* ********************************************************************* */
use if (LTG<.) using $Work/FamaFrench05bb, clear
* add returns
merge 1:1 permco permno datem using $Work/FamaFrench02Ret, keepusing (permco permno datem mcap0-mcap60 ret1-ret60)
keep if _merge==3
drop _merge

* add earnings (generated by earnings.do)
merge 1:1 permco permno datem using $Work/earnings
drop if _merge==2
drop _merge
* *********************************************************** */
bys datem:astile RNK=LTG  , nq(10) 
/* **************************************************************** */
keep permco permno datem RNK *street streetLTM* shs* LTG medest* mcap* ret* nsecurities
tempfile tmp
save "`tmp'", replace
/* **************************************************************** */
forvalues x=1(1)60 {
	local j=`x'-1
	bys RNK datem: egen TM=total(mcap`j' * (!missing(ret`x')))  
	bys RNK datem: gen vwret`x'=ret`x'*(mcap`j'/TM)
	drop TM
	quietly: gen EP`x'=F`x'street if mcap`x'<.
}
collapse (sum) EP* vw*, by(RNK datem)
forvalues x=1(1)60  {
	quietly: replace    EP`x'=. if    EP`x'==0
	quietly: replace vwret`x'=. if vwret`x'==0
}
merge 1:m datem RNK using "`tmp'"
drop _merge
/* **************************************************************** */
*#2.b) find data on distributions when delisted
merge m:1 permco permno using $Work/Delistings02
keep if _merge==3
drop _merge
/* **************************************************************** */
order permco permno datem dlstmo dlstcd distrib 
/* **************************************************************** */
*#2.c) WORK with relevant obs
gen T=dlstmo-datem
keep if T>0 & T<61

su T, d
gen capital0=.
forvalues f=1(1)60 {
	local i=`f'-1
	quietly: generate capital`f'=distrib if `f'==T
	quietly: replace  capital`f'=capital`i'*(1+vwret`f') if `f'>T
	quietly: generate  E`f'=(capital`f'/mcap`f')*EP`f'
}
forvalues x=1(1)5 {
	local t=12*`x'
	quietly: gen REINV`x'=E`t'
}
keep permco permno datem dlstmo  REINV1 REINV2 REINV3 REINV4 REINV5
order permco permno datem dlstmo REINV1 REINV2 REINV3 REINV4 REINV5

merge 1:1 permco permno datem using "`tmp'"
drop if _merge==1
drop _merge
* ********** 
* forecasts
generate f1=medest1*shs0/1000	if nsecurities==1 
generate f2=medest2*shs0/1000	if nsecurities==1	 
generate f3=medest3*shs0/1000	if nsecurities==1
generate f4=medest4*shs0/1000	if nsecurities==1	 
generate f5=medest5*shs0/1000	if nsecurities==1	 

replace f2=(1+LTG/100)*f1 if LTG>-100 & f1>0 & f2==.
replace f3=(1+LTG/100)*f2 if LTG>-100 & f2>0 & f3==.
replace f4=(1+LTG/100)*f3 if LTG>-100 & f3>0 & f4==.
replace f5=(1+LTG/100)*f4 if LTG>-100 & f4>0 & f5==.

* earnings (eps x #shares when forecasts were made)
generate e1=streetLTM1*(shs0/shs1)
generate e2=streetLTM2*(shs0/shs2)
generate e3=streetLTM3*(shs0/shs3)
generate e4=streetLTM4*(shs0/shs4)
generate e5=streetLTM5*(shs0/shs5)

forvalues x=1(2)5 {
	generate new_e`x'=e`x'
	replace  new_e`x'=REINV`x'	if e`x'==. & (dlstmo>=datem)
	drop e`x'
}
tsset permco datem
generate nume1=new_e1 		if f1+new_e1<.
generate base1=f1 	 	if f1+new_e1<.

generate nume3=new_e3	 	if LTG+new_e3+L3.streetLTM<.
generate base3=L3.streetLTM 	if LTG+new_e3+L3.streetLTM<.

generate nume5=new_e5 		if LTG+new_e5+L3.streetLTM<.
generate base5=L3.streetLTM 	if LTG+new_e5+L3.streetLTM<.

gen ege1=ln(nume1)/1-ln(base1)/1
gen ege3=ln(nume3)/3-ln(base3)/3 - LTG/100
gen ege5=ln(nume5)/5-ln(base5)/5 - LTG/100

foreach x of varlist ege1 ege3 ege5 {
	quietly: bys datem: egen max=pctile(`x'), p(99)
	quietly: bys datem: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min
}	
************************************
collapse (mean) ege1 ege3 ege5, by(datem RNK)
foreach Z in ege1 ege3 ege5 {
	replace `Z'=. if `Z'==0
}
foreach x of varlist ege1 ege3 ege5 {
	quietly: bys RNK: egen max=pctile(`x'), p(99)
	quietly: bys RNK: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min 
}
************************************
save $Work/XSection04, replace
